<?php
/*
 * @分销商城
 * @功能说明：商品管理中心
 * @更新说明：暂无更新
 * @文件名 GoodsController.class.php
 * @编码 UTF-8
 * @创建时间 2015-12-25 上午11:50:21
 * @创建人 XING
 */
namespace bj\Controller;

use Think\Controller;

class GoodsController extends ApibaseController
{
    /*
     * 分销商城_商品管理
     * @date: 2015-12-25 上午11:50:41
     * @editor: XING
     */
    public function index() {
        /*参数初始化*/
        $page = I('get.page') ?: 1;
        $perpage = 10;
        $list = M('GoodsBjie')->where(array('delete_at' => '0', 'goods_id' => '0'));
        $url = array();
        if (I('get.goods_title')) {
            $list = $list->where('goods_title LIKE "%' . I('get.goods_title') . '%"');
            $url['goods_title'] = I('get.goods_title');
        }
        if (I('get.goods_skuid')) {
            $list = $list->where(array('goods_skuid' => I('get.goods_skuid')));
            $url['goods_skuid'] = I('get.goods_skuid');
        }
        if (I('get.brand')) {
            $list = $list->where('brand LIKE "%' . I('get.brand') . '%"');
            $url['brand'] = I('get.brand');
        }
        $sObject = clone $list;
        $getData = $list->order(array('add_at' => 'desc'))->page($page, $perpage)->select();
        $this->assign('page', setPage($sObject->count(), $perpage, $page, '?' . http_build_query($url)));
        $this->assign('getData', $getData);

        if (I('get.ref'))
            $this->display('goods/sindex');
        else
            $this->display('goods/index');
    }

    /*
     * 分销商城_商品在线管理
     * @date: 2016-04-27 10:37:00
     * @editor: WENCUN
     * */
    public function online() {
        $page = I('get.page') ?: 1;
        $perpage = 10;
        $url = array();
        $sql = "SELECT a.goods_skuid,b.taxes,b.goods_id,b.goods_title,b.goods_pic,
					   b.sales,b.goods_attr,b.goods_price,b.channel_price,b.kcun,b.add_at
				FROM fx_goods_bjie a
				LEFT JOIN fx_goods_entity b
				ON a.goods_id=b.goods_id
				WHERE b.resource=2 AND b.delete_at = 0";
        if (I('get.goods_title')) {
            $sql = $sql . " AND b.goods_title LIKE '%" . I('get.goods_title') . "%'";
            $url['goods_title'] = I('get.goods_title');
        }
        if (I('get.goods_skuid')) {
            $sql = $sql . " AND a.goods_skuid = '" . I('get.goods_skuid') . "'";
            $url['goods_skuid'] = I('get.goods_skuid');
        }
        if (I('get.brand')) {
            $sql = $sql . " AND b.brand LIKE '%" . I('get.brand') . "%'";
            $url['brand'] = I('get.brand');
        }
        if (I('get.kcun') && I('get.kcun') == '1') {
            $sql = $sql . " AND b.kcun = 0";
            $url['kcun'] = I('get.kcun');
        } else if (I('get.kcun') == '2') {
            $sql = $sql . " AND b.kcun > 0";
            $url['kcun'] = I('get.kcun');
        }
        $goods = CQuery($sql . " order by b.add_at DESC limit " . ($page - 1) * $perpage . "," . $perpage);

        $this->assign('page', setPage(count(CQuery($sql)), $perpage, $page, '?' . http_build_query($url)));
        $this->assign('getData', $goods);
        $this->display('/goods/online');
    }

    /*
     * 分销商城_仓库更新在线商品库存
     * @date: 2016-04-27 11:25:00
     * @editor: WENCUN
     * */
    public function setKcun() {
        if (IS_POST) {
            if (I('goods_id') && I('dosubmit')) {
                $data = array(
                    'channel_price' => I('post.channel_price'),
                    'kcun'          => I('post.kcun')
                );
                $setKcun = M('GoodsEntity')->where(array('goods_id' => I('post.goods_id')))->setField($data);
                if ($setKcun) {
                    M('GoodsBjie')->where(array('goods_id' => I('goods_id')))->setField(array('kcun' => I('post.kcun')));
                    $this->showtips('在线商品库存更新成功');
                } else {
                    $this->showtips('操作失败');
                }
            }
        }
        if (I('get.goods_id')) {
            $sql = "SELECT b.goods_skuid,a.goods_id,a.goods_title,a.goods_price,a.channel_price,a.goods_attr,a.kcun
					FROM fx_goods_entity a
					LEFT JOIN fx_goods_bjie b
					ON a.goods_id=b.goods_id
					WHERE a.resource=2 AND a.goods_id=" . I('get.goods_id');
            $getData = CQuery($sql);
            $this->assign('getData', $getData[0]);
        }
        $this->display('/goods/setkcun');
    }

    /*
     * 分销商城_添加商品
     * @date: 2016-04-25 10:16:16
     * @editor: XING
     */
    public function addGoods() {
        if (I('post.dosubmit')) {
            if (I('post.goods_title') == '') {
                $this->showtips('商品名称有误');
            }
            if (I('post.goods_price') <= '0') {
                $this->showtips('商品价格有误');
            }
            if (I('post.goods_attr') <= '0') {
                $this->showtips('商品规格有误');
            }
            if (I('post.goods_id')) {
                $upData = array(
                    'goods_title'   => trim(I('post.goods_title')),
                    'goods_skuid'   => trim(I('post.goods_skuid')),
                    'goods_price'   => trim(I('post.goods_price')),
                    'kcun'          => trim(I('post.kcun')),
                    'brand'         => trim(I('post.brand')),
                    'taxes'         => I('post.taxes') ?: 2,
                    'goods_pic'     => trim(I('post.goods_pic')),
                    'goods_pic2'    => trim(I('post.goods_pic2')),
                    'goods_pic3'    => trim(I('post.goods_pic3')),
                    'goods_attr'    => trim(I('post.goods_attr')),
                    'goods_content' => htmlspecialchars(I('post.content')),
                    'update_at'     => time()
                );
                $effectId = M('GoodsBjie')->where(array('id' => I('post.goods_id')))
                    ->setField($upData);
                if ($effectId) {
                    $this->showtips('商品信息更新成功');
                } else {
                    $this->showtips('操作失败');
                }
            } else {
                $data = array(
                    'goods_title'   => trim(I('post.goods_title')),
                    'goods_skuid'   => trim(I('post.goods_skuid')),
                    'goods_price'   => trim(I('post.goods_price')),
                    'kcun'          => trim(I('post.kcun')),
                    'brand'         => trim(I('post.brand')),
                    'taxes'         => I('post.taxes') ?: 2,
                    'goods_pic'     => trim(I('post.goods_pic')),
                    'goods_pic2'    => trim(I('post.goods_pic2')),
                    'goods_pic3'    => trim(I('post.goods_pic3')),
                    'goods_attr'    => trim(I('post.goods_attr')),
                    'goods_content' => htmlspecialchars(I('post.content')),
                    'add_at'        => time(),
                    'update_at'     => time()
                );
                $addId = M('GoodsBjie')->add($data);
                if ($addId) {
                    $this->showtips('入库商品成功');
                } else {
                    $this->showtips('操作失败');
                }
            }
        }
        $getLevels = M('CateEntity')
            ->where(array('parent_id' => '0'))->select();
        $this->assign('cate', $getLevels);
        if (I('get.goods_id')) {
            $getData = M('GoodsBjie')
                ->where(array('id' => I('get.goods_id')))->find();
            $this->assign('getData', $getData);
        }
        $this->display('goods/addgoods');
    }

    /*
     * 分销商城_商品删除
     * @date: 2015-12-25 上午11:50:52
     * @editor: XING
     */
    public function deGoods() {
        $delBj = M('GoodsBjie')
            ->where(array('id' => I('get.goods_id'), 'goods_id' => '0'))->delete();
        if ($delBj) {
            putlog('商品删除操作-' . session('loginUid') . '/' . session('loginName'), 'GoodsController', 'deGoods');
            $this->showtips('删除成功');
        } else {
            $this->showtips('删除失败');
        }
    }

    /*
     * 分销商城_分类AJAX
     * @date: 2015-12-25 下午1:21:34
     * @editor: XING
     */
    public function cates() {
        if (I('get.ajaxFrom')) {
            $cates = M('CateEntity')->where(array(
                'parent_id' => I('get.cateid')
            ))->order(array('sort' => 'desc'))->select();

            jsonRespons($cates ?: 0);
        }
    }

    /*
     * 分销商城_查询价格
     * @date: 2016-3-25 下午7:53:33
     * @editor: XING
     */
    public function gprice() {
        if (I('get.price')) {
            $arrProfit = explode(',', getSysConfig('levelRate'));
            echo json_encode(array('price1' => $arrProfit[0] * I('get.price'), 'price2' => $arrProfit[1] * I('get.price'), 'price3' => $arrProfit[2] * I('get.price')));
            exit;
        }
    }

}